<?php
include_once('ra_global.php');
include_once 'site_config.php';

$result = array();
if (!$_GET['mobile']) {
	$result['code'] = 101;
	$result['msg'] = '未传入手机号';
} else {
	$mobile = $_GET['mobile'];
	
	$promocode = $db->ig2_want('mae_promocode', 'mobile = "'. $mobile . '"');
	if ($promocode) {
		$result['code'] = 102;
		$result['msg'] = '该手机号已经领取过优惠码';
	} else {
		$header = array();
		$header['mobile'] = 'mobile';
		$header['promocode'] = 'promocode';
		$header['status'] = 'status';
		
		$promocode = array();
		$code = rand(1,99) . jinzhih_G0($mobile, 36, 8);
		$promocode['mobile'] = $mobile;
		$promocode['promocode'] = $code;
		$promocode['status'] = 0;
		
		$db->ig2_insert3('mae_promocode', $header, $promocode);
		
		$result['code'] = 0;
		$result['msg'] = '成功获取优惠码';
		$result['promocode'] = $code;
	}
}

echo json_encode($result);


function jinzhih_G0($shu,$jinzhi,$w)
         {
         $zifu = "";
         while ($shu!=0){
         $linshi = bcmod($shu,$jinzhi);
         switch ($jinzhi){
             case 2:
                 $zifu = decbin($shu);
                 return $zifu;
               case 8:
                 $zifu = decoct($shu);
                 return $zifu;
             case 16:
                 $zifu = dechex($shu);
                 return $zifu;
             case 36:
                 if ($linshi>=10)
                  {
                  $zifu.= chr(($linshi+55));
                  }else{
                        $zifu.= $linshi;}
                 break;
             case 62:
                  if (($linshi>=10) && ($linshi<36)) {$zifu.= chr($linshi+55);break;}
                  if (($linshi>=36) && ($linshi<62)) {$zifu.= chr($linshi+61);break;}
                  $zifu.= $linshi;break;
         default:
         $zifu.= $linshi;
         break;
         }
         $shu = intval(bcdiv($shu,$jinzhi));
         }
         for ($i=strlen($zifu);$i<$w;$i++)
         $zifu.="0";
        return strrev($zifu);}
?>